//
//  XLUserDataCell.m
//  XLPersonCenter
//
//  Created by 宋振 on 2018/7/23.
//  Copyright © 2018年 夜幕繁华. All rights reserved.
//

#import "XLUserDataCell.h"

#import "XLPersonCenterTool.h"

#import "Masonry.h"

@interface XLUserDataCell ()

//标题
//@property (nonatomic, strong) UILabel *  titleLbl;

//心聊ID---已修改为年龄
@property (nonatomic, strong) UILabel *  XLAgeLbl;

//地区
@property (nonatomic, strong) UILabel *  areaLbl;

//个人介绍
@property (nonatomic, strong) UILabel *  introduceLbl;

//关注ta
@property (nonatomic, strong) UIButton *  attentionBt;

//性别
@property (nonatomic, strong) UILabel *  genderLbl;


@end

@implementation XLUserDataCell
{
    //cell 高度
    CGFloat cellHeight;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        /**
         设置界面显示
         */
        [self setCustomeView];
    }
    
    return self;
}

#pragma mark    - - - 设置界面显示 - - -
- (void)setCustomeView
{
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - kScale(30);
    //标题
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.textColor = [UIColor colorWithRed:17/255.0 green:17/255.0 blue:17/255.0 alpha:1];
    titleLbl.font = [UIFont systemFontOfSize:kScale(13)];
    [self.contentView addSubview:titleLbl];
    
    titleLbl.text = @"个人信息";
    
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(kScale(15));
        make.left.equalTo(self.mas_left).offset(kScale(15));
        make.height.mas_equalTo(kScale(18));
        
    }];
    
    //性别
    [self.contentView addSubview:self.genderLbl];
    //关注按钮
    [self.contentView addSubview:self.attentionBt];
    //年龄
    [self.contentView addSubview:self.XLAgeLbl];
    //地区
    [self.contentView addSubview:self.areaLbl];
    //个人介绍
    [self.contentView addSubview:self.introduceLbl];
    
    
    
    
    //关注按钮
    [self.attentionBt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLbl.mas_bottom);
        make.right.equalTo(self.contentView.mas_right).offset(-kScale(15));
        make.size.mas_equalTo(CGSizeMake(82, 32));
    }];
    
    //性别
    [self.genderLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLbl.mas_left);
        make.top.equalTo(titleLbl.mas_bottom).offset(kScale(5));
        make.right.equalTo(self.mas_right).offset(-kScale(15));
    }];
    
    NSAttributedString *idAttribute = [[NSAttributedString alloc] initWithString:@"年龄：" attributes:@{NSKernAttributeName:@1}];
    
    self.XLAgeLbl.attributedText = idAttribute;
    
    [self.XLAgeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.genderLbl.mas_left);
        make.top.equalTo(self.genderLbl.mas_bottom).offset(kScale(4));
        make.right.equalTo(self.mas_right).offset(-kScale(15));
    }];
    
    //地区
    //    self.areaLbl.text = @"地区：北京市 朝阳区 大沽路村 208号";
    
    NSAttributedString *areaAttribute = [[NSAttributedString alloc] initWithString:@"地区：" attributes:@{NSKernAttributeName:@1}];
    self.areaLbl.attributedText = areaAttribute;
    
    [self.areaLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLbl.mas_left);
        make.top.equalTo(self.XLAgeLbl.mas_bottom).offset(kScale(4));
        make.right.equalTo(self.XLAgeLbl.mas_right);
        make.width.lessThanOrEqualTo(@(maxWidth));
    }];
    
    //个人介绍
    //    self.introduceLbl.text = @"个人介绍：你们尽管送礼物吧，你们送多少我就收多少，然后我就可以发财啦，很开心哇 嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻";
    
    NSAttributedString *introduceAttribute = [[NSAttributedString alloc] initWithString:@"个人介绍：" attributes:@{NSKernAttributeName:@1}];
    
    self.introduceLbl.attributedText = introduceAttribute;
    
    [self.introduceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLbl.mas_left);
        make.top.equalTo(self.areaLbl.mas_bottom).offset(kScale(4));
        make.right.equalTo(self.areaLbl.mas_right);
        make.width.lessThanOrEqualTo(@(maxWidth));
    }];
    
}

- (CGFloat)getCellHeight
{
    [self layoutIfNeeded];
    
    [self layoutSubviews];
    
    CGFloat cellHeight = self.introduceLbl.frame.origin.y + self.introduceLbl.frame.size.height + kScale(15);
    
    return cellHeight;
}


#pragma mark     - - - Actions - - -

//关注事件
- (void)followAction:(UIButton *)sender
{
    NSLog(@"点击关注");
    
    NSString *userId = [NSString stringWithFormat:@"%@",self.userInfoModel.UserId];
    
    //发送关注信息
    [[[XLHTTPSessionManager sharedManager] XLV2POST:@"/api/v2/UserInfo/Follow" parameters:@{@"FollowId":K_DEFULT(userId),@"FansId":[XLAccountManager shareInstance].model.Id} resultClass:nil] subscribeNext:^(id x) {
        
        NSLog(@"关注成功%@",x);
        [self.attentionBt setAttributedTitle:[self setTitleAttributeWithString:@"已关注"] forState:(UIControlStateNormal)];
        
        
    } error:^(NSError *error) {
        
        NSLog(@"关注失败%@",error);
        [HelpTool startTipViewWithText:@"关注失败" duration:1.0 type:(kTipViewTypeNone)];
        
    }];
    
}







#pragma mark    - - - 懒加载 - - -

//性别
- (UILabel *)genderLbl
{
    if (!_genderLbl) {
        self.genderLbl = [[UILabel alloc] init];
        self.genderLbl.font = [UIFont fontWithName:KFontNamePFMedium size:kScale(15)];
        self.genderLbl.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
    }
    
    return _genderLbl;
}

//年龄
- (UILabel *)XLAgeLbl
{
    if (!_XLAgeLbl) {
        self.XLAgeLbl = [[UILabel alloc] init];
        self.XLAgeLbl.font = [UIFont fontWithName:KFontNamePFMedium size:kScale(15)];
        self.XLAgeLbl.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
    }
    
    return _XLAgeLbl;
}

//地区
- (UILabel *)areaLbl
{
    if (!_areaLbl) {
        self.areaLbl = [[UILabel alloc] init];
        self.areaLbl.numberOfLines = 0;
        self.areaLbl.font = [UIFont fontWithName:KFontNamePFMedium size:kScale(15)];
        self.areaLbl.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
    }
    
    return _areaLbl;
}

//个人介绍
- (UILabel *)introduceLbl
{
    if (!_introduceLbl) {
        self.introduceLbl = [[UILabel alloc] init];
        self.introduceLbl.numberOfLines = 0;
        self.introduceLbl.font = [UIFont fontWithName:KFontNamePFMedium size:kScale(15)];
        self.introduceLbl.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
    }
    
    return _introduceLbl;
}

//关注
- (UIButton *)attentionBt
{
    if (!_attentionBt) {
        self.attentionBt = [[UIButton alloc] init];
        self.attentionBt.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 4);
        self.attentionBt.titleEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 0);
        
        //修饰
        self.attentionBt.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.attentionBt setAttributedTitle:[self setTitleAttributeWithString:@"关注Ta"] forState:(UIControlStateNormal)];
        [self.attentionBt setTitleColor:RGB(102, 102, 102) forState:(UIControlStateNormal)];
        [self.attentionBt setImage:[UIImage imageNamed:@"common_icon_guanzhu_n"] forState:(UIControlStateNormal)];
        self.attentionBt.backgroundColor = RGB(226, 226, 226);
        [self.attentionBt addTarget:self action:@selector(followAction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.attentionBt.layer.cornerRadius = 4;
        self.attentionBt.layer.masksToBounds = YES;
        self.attentionBt.hidden = YES;
        
    }
    
    return _attentionBt;
}


#pragma mark    - - - 更新数据信息 - - -
- (void)setUserInfoModel:(XLUserInfoDetailModel *)userInfoModel
{
    _userInfoModel = userInfoModel;
    
    
    if (userInfoModel) {
        
        //是否已经关注
        
        BOOL isSelf = [[NSString stringWithFormat:@"%@",userInfoModel.UserId] isEqualToString:[[XLAccountManager shareInstance] model].Id];
        
        if (isSelf) {
            self.attentionBt.hidden = YES;
        }else{
            self.attentionBt.hidden = NO;
        }
        
        NSString *attention;
        
        if (userInfoModel.IsAttention) {
            attention = @"已关注";
            self.attentionBt.hidden = YES;
        }else{
            attention = @"关注Ta";
            self.attentionBt.hidden = NO;
        }
        [self.attentionBt setAttributedTitle:[self setTitleAttributeWithString:attention] forState:(UIControlStateNormal)];
        
        
        
        
        //性别
        NSString *sex = [NSString stringWithFormat:@"%@",userInfoModel.Sex];
        if ([sex isEqualToString:@"0"])
        {
            sex = @"男";
        }else{
            sex = @"女";
        }
        
        self.genderLbl.attributedText = [self setAttributeWithString:[NSString stringWithFormat:@"性别：%@",K_DEFULT(sex)]];
        
        NSString *age = [userInfoModel.Age stringValue];
        
        self.XLAgeLbl.attributedText = [self setAttributeWithString: [NSString stringWithFormat:@"年龄：%@",K_DEFULT(age)]];
        
        self.areaLbl.attributedText = [self setAttributeWithString:[NSString stringWithFormat:@"地区：%@",K_DEFULT(userInfoModel.HostCity)]];
        
        
        self.introduceLbl.attributedText = [self setAttributeWithString:[NSString stringWithFormat:@"个人介绍：%@",K_DEFULT(userInfoModel.Introduction)]];
    }else{
        self.attentionBt.hidden = YES;
    }
    //设计修改 关注按钮放到头部
    self.attentionBt.hidden = YES;
}

//label
- (NSAttributedString *)setAttributeWithString:(NSString *)string
{
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:string attributes:@{NSKernAttributeName:@1}];
    
    return attribute;
}

//button
- (NSAttributedString *)setTitleAttributeWithString:(NSString *)string
{
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:string attributes:@{
                                                                                                   NSKernAttributeName:@1,
                                                                                                   NSForegroundColorAttributeName:RGB(17, 17, 17)
                                                                                                   }];
    
    return attribute;
}




- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
